﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using IFactory;

public partial class Admin_Department_Department_Manager : System.Web.UI.Page
{
    DepartmentInfoManager dllDepartmentInfoManager = new DepartmentInfoManager();
    IBussinessLogic dllUserInfo = new UserInfoManager();
    DataTable _powerTable = new DataTable();
    Authentication dllAuthentication = new Authentication();
    protected void Page_Load(object sender, EventArgs e)
    {
        dllAuthentication.isAdminLogin();
        this.btnDel.Enabled = false;
        this.btnEditCompany.Enabled = false;
        this.btnMove.Enabled = false;
        this.btnNew.Enabled = false;
        this.btnOK.Enabled = false;
        Model.UserInfo objUserInfo = new Model.UserInfo();
        _powerTable = dllAuthentication.getPower(objUserInfo.UserName, "部门管理");
        for (int i = 0; i < _powerTable.Rows.Count; i++)
        {
            if (_powerTable.Rows[i]["PowerName"].ToString() == "新建")
            {
                this.btnNew.Enabled = true;
            }
            if (_powerTable.Rows[i]["PowerName"].ToString() == "删除")
            {
                this.btnDel.Enabled = true;
            }
            if (_powerTable.Rows[i]["PowerName"].ToString() == "查看")
            {
            }
            if (_powerTable.Rows[i]["PowerName"].ToString() == "修改")
            {
                this.btnOK.Enabled = true;
                this.btnMove.Enabled = true;
                this.btnEditCompany.Enabled = true;
            }
        }
        if (!IsPostBack)
        {
            bindTreeView();
        }
    }


    /// <summary>
    /// 绑定TreeView,同时标明状态
    /// </summary>
    /// <author>grokyao</author>
    /// <date>2009-1-15</date>
    void bindTreeView()
    {
        DataTable _department = dllDepartmentInfoManager.get();
        string _comanyName = System.Configuration.ConfigurationManager.AppSettings["CompanyName"].ToString();
        TreeNode _root = new TreeNode(_comanyName, "0");
        this.trvDepartment.Nodes.Add(_root);
        for (int i = 0; i < _department.Rows.Count; i++)
        {
            TreeNode _departmentNode;
            if (_department.Rows[i]["DepartmentState"].ToString() == "1")
            {
                _departmentNode = new TreeNode(_department.Rows[i]["DepartmentName"].ToString());
                _departmentNode.ImageUrl = "~/images/Tree_department1.gif";
                _departmentNode.Text += "(停用状态)";
            }
            else
            {
                _departmentNode = new TreeNode(_department.Rows[i]["DepartmentName"].ToString());
                _departmentNode.ImageUrl = "~/images/Tree_department.gif";
            }
            _departmentNode.Value = _department.Rows[i]["DepartmentId"].ToString();
            _root.ChildNodes.Add(_departmentNode);
            RecursionTreeNode(_departmentNode);
        }
        this.trvDepartment.ExpandDepth = 3;
    }




    /// <summary>
    /// 递归方法
    /// </summary>
    void RecursionTreeNode(TreeNode _parent)
    {
        object[] _value = new object[1];
        _value[0] = Convert.ToInt32(_parent.Value);
        DataTable _department = dllDepartmentInfoManager.get(DepartmentInfoManager.field.DepartmentParentId.ToString(), _value);
        //查询出的下一级子节点先绑定
        for (int i = 0; i < _department.Rows.Count; i++)
        {
            TreeNode _child = new TreeNode();
            _child.ImageUrl = "~/images/Tree_department.gif";
            _child.Text = _department.Rows[i]["DepartmentName"].ToString();
            _child.Value = _department.Rows[i]["DepartmentId"].ToString();
            _parent.ChildNodes.Add(_child);
            if (_department.Rows[i]["DepartmentState"].ToString() == "1")
            {
                _child.Text += "(停用状态)";
                _child.ImageUrl = "~/images/Tree_department1.gif";
            }
            else
            {
                _child.ImageUrl = "~/images/Tree_department.gif";
            }
        }
        //遍历已绑定的该级节点，通过递归判断是否有下级并绑定
        for (int i = 0; i < _parent.ChildNodes.Count; i++)
        {
            TreeNode _child = _parent.ChildNodes[i];
            RecursionTreeNode(_child);
        }
    }





    /// <summary>
    /// TreeView节点选择事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void trvDepartment_SelectedNodeChanged(object sender, EventArgs e)
    {
        //获取选择的TreeView项
        TreeNode tn = this.trvDepartment.SelectedNode;
        string _companyName = System.Configuration.ConfigurationManager.AppSettings["CompanyName"].ToString();
        if (tn != null && tn.Text != _companyName)
        {
            object[] _valueDepartment = new object[1];
            _valueDepartment[0] = Convert.ToInt32(tn.Value);
            DataTable _department = dllDepartmentInfoManager.get(DepartmentInfoManager.field.DepartmentId.ToString(), _valueDepartment);
            if (_department.Rows.Count != 0)
            {
                //根据IsDelete判断是否启用
                if (_department.Rows[0]["DepartmentState"].ToString() == "0")
                {
                    this.rdbTrue.Checked = true;
                    this.rdbFalse.Checked = false;
                }
                else
                {
                    this.rdbTrue.Checked = false;
                    this.rdbFalse.Checked = true;
                }
                //赋值
                this.txtDepartmentName.Text = _department.Rows[0]["DepartmentName"].ToString();
                this.txtDepartmentRemark.Text = _department.Rows[0]["DepartmentRemark"].ToString();
                this.txtDepartmentNo.Text = _department.Rows[0]["DepartmentNo"].ToString();
                this.txtDepartmentSortId.Text = _department.Rows[0]["DepartmentSortId"].ToString();
                this.txtEvaluatePeople.Value = _department.Rows[0]["DefaultEvaluatePeople"].ToString();
                this.txtSlaveDepartment.Value = _department.Rows[0]["SlaveDepartment"].ToString();
                object[] _valueUserInfo = new object[1];
                _valueUserInfo[0] = _department.Rows[0]["DepartmentMaster"].ToString();
                DataTable _user = dllUserInfo.get(UserInfoManager.field.UserName.ToString(), _valueUserInfo);
                if (_user.Rows.Count > 0)
                {
                    this.txtDepartmentMaster.Value = _user.Rows[0]["RealName"].ToString();
                }
                else
                {
                    this.txtDepartmentMaster.Value = "";
                }
            }
        }
        if (tn.Text == _companyName)
        {
        }
    }


    /// <summary>
    /// 修改确认按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <author>grokyao</author>
    /// <date>2009-1-15</date>
    protected void btnOK_Click(object sender, EventArgs e)
    {
        TreeNode tn = this.trvDepartment.SelectedNode;
        if (tn != null)
        {
            object[] _value = new object[9];
            _value[0] = Convert.ToInt32(tn.Value);
            _value[1] = Convert.ToInt32(this.txtDepartmentNo.Text.Trim());
            _value[2] = Convert.ToInt32(this.txtDepartmentSortId.Text.Trim());
            _value[3] = this.txtDepartmentName.Text.Trim();
            object[] _valueUserInfo = new object[1];
            _valueUserInfo[0] = this.txtDepartmentMaster.Value.Trim();
            DataTable _userInfo = dllUserInfo.get(UserInfoManager.field.RealName.ToString(), _valueUserInfo);
            if (_userInfo.Rows.Count > 0)
            {
                _value[4] = _userInfo.Rows[0]["UserName"].ToString();
            }
            else
            {
                _value[4] = "";
            }
            _value[5] = this.txtDepartmentRemark.Text;
            _value[6] = 0;
            if (this.rdbTrue.Checked == true && this.rdbFalse.Checked == false)
            {
                _value[6] = 0;
            }
            if (this.rdbTrue.Checked == false && this.rdbFalse.Checked == true)
            {
                _value[6] = 1;
            }
            _value[7] = this.txtEvaluatePeople.Value;
            _value[8] = this.txtSlaveDepartment.Value;
            int result = dllDepartmentInfoManager.edit(DepartmentInfoManager.field.DepartmentId.ToString(), _value);
            if (result == 0)
            {
                Utility.Error.RedirectErrorPage("修改部门未成功，请重试！");
            }
            else
            {
                Response.Redirect("Department_Manager.aspx");
            }
        }
    }


    /// <summary>
    /// 新建部门事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <author>grokyao</author>
    /// <date>2009-2-13</date>
    protected void btnNew_Click(object sender, EventArgs e)
    {
        TreeNode _departmentParent = this.trvDepartment.SelectedNode;
        string _companyName = System.Configuration.ConfigurationManager.AppSettings["CompanyName"].ToString();
        if (_departmentParent != null)
        {

            ScriptManager.RegisterClientScriptBlock(this.btnNew, GetType(), "open", "window.open('DepartmentAdd.aspx?DepartmentParentId=" + _departmentParent.Value + "','','height=80,width=310,top=300,left =380status=yes,toolbar=no,menubar=no,location=no');", true);

        }
        else
        {
            ScriptManager.RegisterClientScriptBlock(this.btnNew, GetType(), "open", "window.open('DepartmentAdd.aspx?DepartmentParentId=0','','height=80,width=310,top=300,left =380status=yes,toolbar=no,menubar=no,location=no');", true);
        }

    }



    /// <summary>
    /// 删除部门事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <author>grokyao</author>
    /// <date>2009-2-13</date>
    protected void btnDel_Click(object sender, EventArgs e)
    {
        TreeNode _tnSelect = this.trvDepartment.SelectedNode;
        if (_tnSelect != null)
        {
            object[] _value = new object[1];
            _value[0] = Convert.ToInt32(_tnSelect.Value);
            int result = dllDepartmentInfoManager.del(DepartmentInfoManager.field.DepartmentId.ToString(), _value);
            if (result == 0)
            {
                Utility.Error.RedirectErrorPage("删除部门未成功，请重试！");
            }
            else
            {
                Response.Redirect("Department_Manager.aspx");
            }
        }
    }


    /// <summary>
    /// 移动事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnMove_Click(object sender, EventArgs e)
    {
        TreeNode _tnSelect = this.trvDepartment.SelectedNode;
        if (_tnSelect != null)
        {
            ScriptManager.RegisterClientScriptBlock(this.btnNew, GetType(), "open", "window.open('DepartmentMove.aspx?DepartmentId=" + _tnSelect.Value + "','','height=300,width=320,top=100,left =180status=yes,toolbar=no,menubar=no,location=no');", true);

            //Utility.HttpHelp.windowOpen("DepartmentMove.aspx?DepartmentId=" + _tnSelect.Value, Page);
        }
        else
        {
            Utility.HttpHelp.alert("请选择要移动的部门", Page);
        }
    }


    /// <summary>
    /// 修改公司名称事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAddAndReturn_Click(object sender, EventArgs e)
    {
        Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
        AppSettingsSection _appSetting = (AppSettingsSection)config.GetSection("appSettings");
        _appSetting.Settings["CompanyName"].Value = this.txtDepartmentName.Text.Trim();
        config.Save();
        Response.Redirect("Department_Manager.aspx");
    }


    /// <summary>
    /// 修改公司名称
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <author>grokyao</author>
    /// <date>2009-3-3</date>
    protected void btnEditCompany_Click(object sender, EventArgs e)
    {
        string _companyName = System.Configuration.ConfigurationManager.AppSettings["CompanyName"].ToString();
        TreeNode _tn = this.trvDepartment.SelectedNode;
        if (_tn == null)
        {
        }
        else
        {
            if (_tn.Text == _companyName)
            {
                ScriptManager.RegisterStartupScript(this.trvDepartment, this.trvDepartment.GetType(), "edit", "window.open('EditCompanyName.aspx?CompanyName=" + _companyName + "','','height=90,width=330,top=300,left =380status=yes,toolbar=no,menubar=no,location=no')", true);
            }
        }
    }
    protected void btnIntroduction_Click(object sender, EventArgs e)
    {
        TreeNode tn = this.trvDepartment.SelectedNode;
        if (tn != null)
        {
            Response.Redirect("EditDepartmentSlogans.aspx?typeid=1&DepartmentId=" + tn.Value);
        }
    }
    protected void btnCoreevents_Click(object sender, EventArgs e)
    {
        TreeNode tn = this.trvDepartment.SelectedNode;
        if (tn != null)
        {
            Response.Redirect("EditDepartmentSlogans.aspx?typeid=2&DepartmentId=" + tn.Value);
        }
    }
    protected void btnManagementObject_Click(object sender, EventArgs e)
    {
        TreeNode tn = this.trvDepartment.SelectedNode;
        if (tn != null)
        {
            Response.Redirect("EditDepartmentSlogans.aspx?typeid=3&DepartmentId=" + tn.Value);
        }
    }
    protected void btnManagementSlogans_Click(object sender, EventArgs e)
    {
        TreeNode tn = this.trvDepartment.SelectedNode;
        if (tn != null)
        {
            Response.Redirect("EditDepartmentSlogans.aspx?typeid=4&DepartmentId=" + tn.Value);
        }
    }

    protected void btnYearIntroduction_Click(object sender, EventArgs e)
    {
        TreeNode tn = this.trvDepartment.SelectedNode;
        if (tn != null)
        {
            Response.Redirect("EditYearDepartmentSlogans.aspx?typeid=1&DepartmentId=" + tn.Value);
        }
    }
    protected void btnYearCoreevents_Click(object sender, EventArgs e)
    {
        TreeNode tn = this.trvDepartment.SelectedNode;
        if (tn != null)
        {
            Response.Redirect("EditYearDepartmentSlogans.aspx?typeid=2&DepartmentId=" + tn.Value);
        }
    }
    protected void btnYearManagementObject_Click(object sender, EventArgs e)
    {
        TreeNode tn = this.trvDepartment.SelectedNode;
        if (tn != null)
        {
            Response.Redirect("EditYearDepartmentSlogans.aspx?typeid=3&DepartmentId=" + tn.Value);
        }
    }
    protected void btnYearManagementSlogans_Click(object sender, EventArgs e)
    {
        TreeNode tn = this.trvDepartment.SelectedNode;
        if (tn != null)
        {
            Response.Redirect("EditYearDepartmentSlogans.aspx?typeid=4&DepartmentId=" + tn.Value);
        }
    }
}
